package com.example.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.example.domain.entity.Notice;
import com.example.domain.entity.Params;
import com.example.mapper.NoticeMapper;
import com.example.service.NoticeService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class NoticeServiceImpl implements NoticeService {

//    @Resource
//    private NoticeDao noticeDao;

    @Resource
    private NoticeMapper noticeMapper;

    @Override
    public List<Notice> findAll(Params params) {
        // 构建查询条件
        QueryWrapper<Notice> queryWrapper = new QueryWrapper<>();
        if (params != null && params.getStudentName() != null && !params.getStudentName().isEmpty()) {
            queryWrapper.eq("user_id", params.getUserId());
        }
        // 按 id 倒序排序
        queryWrapper.orderByDesc("id");
        // 限制查询结果数量为 6
        queryWrapper.last("limit 6");

        // 执行查询
        return noticeMapper.selectList(queryWrapper);
    }

    @Override
    public Page<Notice> findBySearch(Params params) {
        // 创建 MyBatis-Plus 的分页对象
        Page<Notice> page = new Page<>(params.getPageNum(), params.getPageSize());
        // 创建查询条件包装器
        QueryWrapper<Notice> queryWrapper = new QueryWrapper<>();
        // 按 id 倒序排序
        queryWrapper.orderByDesc("id");

        // 执行分页查询
        Page<Notice> noticePage = noticeMapper.selectPage(page, queryWrapper);

        // 将 MyBatis-Plus 的 Page 对象转换为 PageInfo 对象
        return noticePage;
    }

    @Override
    public void add(Notice type) {
        noticeMapper.insert(type);
    }

    @Override
    public void update(Notice type) {
        noticeMapper.updateById(type);
    }

    @Override
    public void delete(Integer id) {
        noticeMapper.deleteById(id);
    }

    @Override
    public Notice findById(Integer id) {
        return noticeMapper.selectById(id);
    }
}